package com.dfs.parent.service.controller;

import com.dfs.parent.common.core.domain.Result;
import com.dfs.parent.service.dto.ProductDTO;
import com.dfs.parent.service.property.MyProperties;
import com.dfs.parent.service.vo.ProductVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 仓储服务 前端控制器
 * </p>
 *
 * @author lihaodong
 * @since 2019-11-25
 */
@RestController
@RefreshScope
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {
    private final ApplicationContext applicationContext;
    @Autowired
    private MyProperties myProperties;

    @Value("${user.name:}")
    private String name;

    @Value("${user.age:}")
    private String age;
    @GetMapping("/config")
    public Result<String> config() {
        String port=applicationContext.getEnvironment().getProperty("server.port");
        return Result.ok(name + "--port:" + port);
    }

    @GetMapping("/config2")
    public String config2() {
        return myProperties.getName() + "--" + myProperties.getAge();
    }

    /**
     * 扣减库存
     */

    @PostMapping("/reduceCount")
    public Result<ProductVO> reduceCount(@RequestBody ProductDTO productDTO, HttpServletRequest request) throws Exception {

        System.out.println(request);
//            int a = 10/0;
        System.out.println("ProductVO start===>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        System.out.println("我是8091======start===================");
//        try {
//            throw new Exception("猜测是重试");
////            Thread.sleep(8000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("我是8091=====8秒end====================");
        ProductVO productVO = new ProductVO();
        productVO.setCount(2222);
        productVO.setId(productDTO.getId());
        productVO.setPrice(productDTO.getPrice());
        System.out.println("ProductVO end===>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return Result.ok(productVO);
    }

    @RequestMapping("/save")
    public Result save(@RequestParam Integer count, @RequestParam Integer productId) throws Exception {
        System.out.println("###save===");
        return Result.ok("success");
    }


    @PostMapping("/parse1")
    public Result parse1( @RequestBody ProductDTO productDTO) throws Exception {
        System.out.println("8091=======>/parse1");
//        int a= 1/0;
//        Thread.sleep(3000);
        productDTO.setName("parse11111>name=="+System.currentTimeMillis());
        return Result.ok(productDTO);
    }

    @PostMapping("/parse2")
    public Result parse2(@RequestBody ProductDTO productDTO) throws Exception {
        System.out.println("=======>/parse2");
//        Thread.sleep(3000);
        productDTO.setName("parse22222>name=="+System.currentTimeMillis());

        return Result.ok(productDTO);
    }


}

